package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.JdbcDao;
import com.dao.Page;
import com.dao.User;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String methodName = request.getParameter("methodName");
		int method = Integer.parseInt(methodName);
		if (method == 0) {
			try {
				this.insert(request, response);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method==1) {
			try {
				this.difpage(request, response);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method==2) {
			try {
				this.delete(request, response);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method == 5) {
			try {
				this.findUser(request, response);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method == 4) {
			try {
				this.checkUser(request, response);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method == 3) {
			try {
				this.updateUser(request, response);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	public void insert(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, Exception {
		Connection conn = null;
		Statement stat = null;
		String goodsname = request.getParameter("goodsname").trim();
		double	goodsprice = Double.parseDouble(request.getParameter("goodsprice").trim());
		int goodsid = Integer.parseInt(request.getParameter("goodsid").trim());
		String thedate = request.getParameter("thedate").trim();
		String shelflife = request.getParameter("shelflife").trim();

		conn = JdbcDao.connection();
		stat = conn.createStatement();
		
		stat.execute("insert into superket(goodsid,goodsname,goodsprice,thedate,shelflife) values('"+goodsid+"','"
				 + goodsname + "','" + goodsprice + "','" + thedate +"','"+shelflife+ "')");
		JdbcDao.close(stat, conn);
		difpage(request,response);
	}

	public void difpage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ClassNotFoundException, SQLException {
		
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		conn = JdbcDao.connection();
		stat = conn.createStatement();
		ArrayList<User> result = new ArrayList<User>();
		rs=stat.executeQuery("select * from superket");
		
		while(rs.next())
        {
        	User st=new User();
        	st.setGoodsid(rs.getInt("goodsid"));
        	st.setGoodsname(rs.getString("goodsname"));
        	st.setGoodsprice(rs.getDouble("goodsprice"));
        	st.setThedate(rs.getString("thedate"));
        	st.setShelflife(rs.getString("shelflife"));
        	
        	
        	result.add(st);	
        }
	    if(rs!=null){
	    	  rs.close();
	       }
	    JdbcDao.close(stat,conn);

		request.setAttribute("subResult", result);
		request.getRequestDispatcher("selectUser.jsp").forward(request, response);
	}
	
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException{
    	Connection conn=null;
    	Statement stat=null;
    	conn=JdbcDao.connection();
 		stat=conn.createStatement();
 		String id=request.getParameter("id");
		stat.execute("delete from superket where goodsid="+id+"");
		JdbcDao.close(stat,conn);
		this.difpage(request, response);
    } 
    
    public void findUser(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
    	Connection conn=null;
    	Statement stat=null;
    	conn=JdbcDao.connection();
 		stat=conn.createStatement();
 		ResultSet rs = null;
 		int id =0;
 		ArrayList<User> result = new ArrayList<User>();
 		String goodsid = request.getParameter("id"); 		
 		if(!"".equals(goodsid)) {
 			id = Integer.parseInt(goodsid);
 		} 		
 		String name = request.getParameter("name").trim();
 		if("".equals(goodsid) && "".equals(name)) {
 			rs = stat.executeQuery("select * from superket");
 		}else {
 			rs = stat.executeQuery("select * from superket where goodsid="+id+"");
 		}
 		
 		while(rs.next())
        {
        	User st=new User();
        	st.setGoodsid(rs.getInt("goodsid"));
        	st.setGoodsname(rs.getString("goodsname"));
        	st.setGoodsprice(rs.getDouble("goodsprice"));
        	st.setThedate(rs.getString("thedate"));
        	st.setShelflife(rs.getString("shelflife"));
        	
        	result.add(st);	
        }
	    if(rs!=null){
	    	  rs.close();
	       }
	    JdbcDao.close(stat,conn);
	    request.setAttribute("subResult", result);
		request.getRequestDispatcher("selectUser.jsp").forward(request, response);
    }
    
    public void checkUser(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, IOException, ServletException {
    	int id = Integer.parseInt(request.getParameter("id").trim());
    	Connection conn=null;
    	Statement stat=null;
    	conn=JdbcDao.connection();
 		stat=conn.createStatement();
 		ResultSet rs = null;
 		int goodsid=0;
 		String goodsname = null;
 		double goodsprice=0;
 		String thedate=null;
 		String shelflife=null;
 		rs = stat.executeQuery("select * from superket where goodsid="+id+"");
 		while(rs.next())
        {        	
 			goodsid = rs.getInt("goodsid");
 			goodsname = rs.getString("goodsname");
 			goodsprice=rs.getDouble("goodsprice");
 			thedate = rs.getString("thedate"); 
 			shelflife = rs.getString("shelflife"); 
        }
	    if(rs!=null){
	    	  rs.close();
	       }
	    JdbcDao.close(stat,conn);
	    request.setAttribute("id",goodsid);		 
    	request.getRequestDispatcher("updateUser.jsp").forward(request, response);
    }
    
    
    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
    	Connection conn = null;
		Statement stat = null;
		String goodsname = request.getParameter("goodsname").trim();
		double	goodsprice = Double.parseDouble(request.getParameter("goodsprice").trim());
		String thedate = request.getParameter("thedate").trim();
		String shelflife = request.getParameter("shelflife").trim();
		int goodsid = Integer.parseInt(request.getParameter("goodsid").trim());

		conn = JdbcDao.connection();
		stat = conn.createStatement();		
		stat.execute("update superket set goodsname="+"'"+goodsname+"',goodsprice='"+goodsprice+"',thedate="+"'"+thedate+"',shelflife="+"'"+shelflife+"' where goodsid="
				+goodsid+"");
		JdbcDao.close(stat, conn);
		difpage(request,response);
    }

}
